home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTSEARCH.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  91 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btsearch.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "btree_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btsearch - search a btree
  20.  
  21. SYNOPSIS
  22.      #include <btree.h>
  23.  
  24.      int btsearch(btp, buf)
  25.      btree_t *btp;
  26.      const void *buf;
  27.  
  28. DESCRIPTION
  29.      The btsearch function searches the btree btp for the key pointed
  30.      to by buf.  If it is found, the cursor is set to the location of
  31.      the key and 1 is returned.  If it is not found, the cursor is set
  32.      to the next higher key and 0 is returned.
  33.  
  34.      btsearch will fail if one or more of the following is true:
  35.  
  36.      [EINVAL]       btp is not a valid btree pointer.
  37.      [EINVAL]       buf is the NULL pointer.
  38.      [BTELOCK]      btp is not read locked.
  39.  
  40. SEE ALSO
  41.      btdelcur, btdelete, btinsert.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 1 is returned if the key
  45.      was found or a value of 0 if it was not found.  On failure, a
  46.      value of -1 is returned, and errno set to indicate the error.
  47.      
  48. ------------------------------------------------------------------------------*/
  49. #ifdef AC_PROTO
  50. int btsearch(btree_t *btp, const void *buf)
  51. #else
  52. int btsearch(btp, buf)
  53. btree_t *btp;
  54. const void *buf;
  55. #endif
  56. {
  57.     int found = 0;        /* key found flag */
  58.  
  59.     /* validate arguments */
  60.     if (!bt_valid(btp) || buf == NULL) {
  61.         errno = EINVAL;
  62.         return -1;
  63.     }
  64.  
  65.     /* check locks */
  66.     if (!(btp->flags & BTRDLCK)) {
  67.         errno = BTELOCK;
  68.         return -1;
  69.     }
  70.  
  71.     /* search to position to insert */
  72.     found = bt_search(btp, buf);
  73.     if (found == -1) {
  74.         BTEPRINT;
  75.         return -1;
  76.     }
  77.  
  78.     /* check if cursor on empty slot */
  79.     if (found == 0) {
  80.         if (btp->cbtpos.key > btp->cbtnp->n) {
  81.             btp->cbtpos.key = btp->cbtnp->n;
  82.             if (btnext(btp) == -1) {
  83.                 BTEPRINT;
  84.                 return -1;
  85.             }
  86.         }
  87.     }
  88.  
  89.     return found == 1 ? 1: 0;
  90. }
  91.